package com.viphuli.http.handler;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;

import com.offroader.R;
import com.offroader.utils.ViewUtils;
import com.viphuli.adapter.OrderAdapter;
import com.viphuli.fragment.PurchaseOrderFragment;
import com.viphuli.http.bean.page.PurchaseOrderPage;
import com.viphuli.http.bean.part.PurchaseOrder;

public class PurchaseOrderResponseHandler extends MyBaseHttpResponseHandler<PurchaseOrderFragment, PurchaseOrderPage> {

	@Override
	public void deal() {
		List<PurchaseOrder> list = page.getList();
		
		if (list != null && !list.isEmpty()) {//无Adapter,设置并填充数据后通知视图更新
			caller.getEmptyLayout().setVisibility(View.GONE);
			
			caller.setPage(page.getPage());//设置页码
			caller.getList().addAll(list);//填充数据

			if (caller.getListView().getAdapter() == null) {
				
				OrderAdapter adapter = new OrderAdapter(caller, caller.getList());
				
				// 添加只是为了在ListView的setAdapter方法时将Adapter包装成HeaderViewListAdapter。因此并不需要footer，因此添加后再移除
				View mListViewFooter = LayoutInflater.from(caller.getActivity()).inflate(R.layout.listview_footer, null, false);
				caller.getListView().addFooterView(mListViewFooter);
				caller.getListView().setAdapter(adapter);
				caller.getListView().removeFooterView(mListViewFooter);
				
				caller.setBaseAdapter(adapter);
				
			}else {//填充数据后通知视图更新
				
				caller.getBaseAdapter().notifyDataSetChanged();
				
			}

		} else if (page.isFirstPage()) {//加载第一页无数据时，显示空界面
			caller.getEmptyLayout().setVisibility(View.VISIBLE);
		}else{
			ViewUtils.toast(R.string.list_no_more_data);
		}
	}

}
